package com.gdyx.app.web.web;


import com.gdyx.app.web.model.*;
import com.gdyx.app.web.svc.*;
import com.gdyx.app.web.util.Condition;
import com.gdyx.app.web.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by Y_Coffee on 2017/8/3.
 */
@Controller
@RequestMapping("/")
public class ViewController extends BaseController {
    @Autowired
    HttpServletRequest request;
    @Autowired
    MpsUserSvc mpsUserSvc;
    @Autowired
    MpsFunSvc mpsFunSvc;
    @Autowired
    MpsRoleFunSvc mpsRoleFunSvc;
    @Autowired
    MpsRoleOptSvc mpsRoleOptSvc;
    @Autowired
    MpsUserRoleSvc mpsUserRoleSvc;
    @Autowired
    MpsDeptUserSvc mpsDeptUserSvc;
    @Autowired
    MpsDeptRoleSvc mpsDeptRoleSvc;
    @Autowired
    MpsFunOptSvc mpsFunOptSvc;
    @Autowired
    MpsOrgSvc mpsOrgSvc;
    @Autowired
    MpsOrgUserSvc mpsOrgUserSvc;
    @Autowired
    MpsRoleSvc mpsRoleSvc;


    @GetMapping
    public ModelAndView index(ModelMap map) throws Exception {
        ModelAndView view = new ModelAndView();
        MpsUser mpsUser = (MpsUser) request.getSession().getAttribute("ADMIN_SESSION");
        String org_session = (String) request.getSession().getAttribute("Org_SESSION");
        if (null == mpsUser) {
            view.addObject("respMsg", "请先登录");
            view.setViewName("login");
        } else {
            GetFunByUser(mpsUser.getId(),org_session);
            view.setViewName("index");
        }
        return view;
    }

    /***
     * 访问通用的HTML模板
     * @param url
     * @return
     */
    @GetMapping("{view}.html")
    public ModelAndView getHtmlFile(@RequestParam("view") String url) {
        ModelAndView view = new ModelAndView();
        view.setViewName(url);
        if (!view.hasView()) {

        }
        return view;
    }

    @GetMapping("goLogin")
    public ModelAndView goLogin() {
        ModelAndView view = new ModelAndView();
        view.setViewName("login");
        return view;
    }

    @PostMapping("doLogin")
    public String doLogin(String name, String pasv) {
        MpsUser mpsUser = mpsUserSvc.findMpsUserByName(name);
        if (MD5Utils.string2MD5(pasv).equalsIgnoreCase(mpsUser.getPasv())) {
            request.getSession().setAttribute("ADMIN_SESSION", mpsUser);
            return "redirect:/goIndex";
        }else {
            return "redirect:/goLogin";
        }

    }

    /**
     *前方注册页面
     * @param respMsg
     * @return
     */
    @GetMapping("doRegister")
    public ModelAndView doRegister(@ModelAttribute("respMsg") String respMsg){
        ModelAndView view = new ModelAndView();
        view.setViewName("register");
        view.addObject("respMsg", respMsg);
        return view;
    }

    /**
     * 退出销毁
     * @return
     */
    @GetMapping("logout")
    public ModelAndView logOut(){
        ModelAndView view = new ModelAndView();
        request.getSession().invalidate();
        view.setViewName("login");
        return view;
    }

    /**
     *显示机构2个以上的选择
     * @return
     */
    @GetMapping("findOrgNum")
    public ModelAndView  findOrgNum(){
        ModelAndView view=new ModelAndView();
        MpsUser mpsUser = (MpsUser) request.getSession().getAttribute("ADMIN_SESSION");
        //判断机构数量
        List<MpsOrgUser> mpsOrgUsersByUid = mpsOrgUserSvc.findMpsOrgUsersByUid(mpsUser.getId());
        Iterator<MpsOrgUser> iterator = mpsOrgUsersByUid.iterator();
        while (iterator.hasNext()) {
            MpsOrgUser mpsOrgUser = iterator.next();
            if(mpsOrgUser.getOrgchk()!=0 || mpsOrgUser.getUserchk()!=0){
                iterator.remove();
            }
        }
        if(mpsOrgUsersByUid.size()>1){
            //存在两个机构
            Map<String, Object> map = new HashMap<>();
            List<Condition> conditions = new ArrayList<>();
            for (int i = 0; i < mpsOrgUsersByUid.size(); i++) {
                if(i==0){
                    conditions.add(ruleBuild("and", "id", "=",mpsOrgUsersByUid.get(i).getOrgId(), "S"));
                }else {
                    conditions.add(ruleBuild("or", "id", "=",mpsOrgUsersByUid.get(i).getOrgId(), "S"));
                }
                map.put("conditions", conditions);
            }
            List<MpsOrg> mpsOrgList = mpsOrgSvc.findMpsOrgs(map);
            view.addObject("respMsg","系统检测到该用户存在两个机构,请选择一个用户登录");
            view.addObject("mpsOrgList",mpsOrgList);
            view.setViewName("Message");
            return  view;
        }
        view.setViewName("index");
        return view;
    }

    /**
     * 前往主页
     * @return
     */
    @GetMapping("goIndex")
    public ModelAndView goIndex(){
        ModelAndView view = new ModelAndView();
        view.setViewName("index");
        return view;
    }
}
